package com.yugao.fintech.antelope.base.feign;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.yugao.fintech.antelope.base.exception.enums.GlobalErrorEnum;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.response.Resp;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {
    @Override
    public Exception decode(String s, Response response) {
        String body = null;
        try {
            if (response.status() == HttpStatus.NOT_FOUND.value()) {
                return new BizException(GlobalErrorEnum.PATH_NOT_PATH);
            }
            if (response.body() != null) {
                body = Util.toString(response.body().asReader(Util.UTF_8));
                Resp<?> resp = JSONObject.parseObject(body, new TypeReference<Resp<?>>() {
                });
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (Objects.nonNull(requestAttributes)) {
                    HttpServletResponse servletResponse = requestAttributes.getResponse();
                    if (Objects.nonNull(servletResponse)) {
                        servletResponse.setStatus(response.status());
                    }
                }

                return new BizException(resp.getCode(), resp.getMsg());
            }
        } catch (Exception e) {
            if (e instanceof BizException) {
                throw (BizException) e;
            }
            log.error("error: ", e);
        }
        return new BizException(GlobalErrorEnum.INVOKE_OUTSIDE_SYSTEM);
    }
}
